﻿using LdtDataEngin.ModuleDetail.CommonModule.PersonMerger;
using LdtJudge.DataEngine.Common;
using LdtJudge.DataEngine.Common.XmlUtility;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.XMLConfigModel.SituationPloModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace LdtDataEngin.ModuleDetail.BaseModule.MultiFack
{
    public class MultiFackSituationPlot
    {
        /// <summary>
        /// 数据上下文
        /// </summary>
        public DataContext dataContext;

        /// <summary>
        /// 犯罪事实中情形情节
        /// </summary>
        public List<SituationPlotDataModel> crimeSituationPlotDataModels = new List<SituationPlotDataModel>();
        /// <summary>
        /// 相关情形情节
        /// </summary>
        public List<SituationPlotDataModel> relevantSituationPlotDataModels = new List<SituationPlotDataModel>();


        /// <summary>
        /// 情形情节信息合并
        /// </summary>
        public void Situation_fff(List<ElementModel> elementModelList)
        {
            //多人员信息不为空，且存在情形情节
            if (elementModelList.Count > 0 && elementModelList.Where(a => a.situationPlotResultModel != null).ToList().Count > 0)
            {
                List<SituationPlotConvert> crimeDataModelList = new List<SituationPlotConvert>();
                List<SituationPlotConvert> relevantDataModelList = new List<SituationPlotConvert>();

                List<ElementModel> elementSituationList = elementModelList.Where(a => a.situationPlotResultModel != null).ToList();
                foreach (var elementModel in elementSituationList)
                {
                    SituationPlotConvert crimeSituationPlotConvert = PloConvert(elementModel.situationPlotResultModel.crimeDataModelList);
                    if (crimeDataModelList.FirstOrDefault(a => a.Name == crimeSituationPlotConvert.Name) != null)
                    {
                        SituationPlotConvert convert = crimeDataModelList.FirstOrDefault(a => a.Name == crimeSituationPlotConvert.Name);
                        CompareList(crimeSituationPlotConvert.plotIDList, convert.plotIDList, "2");
                        CompareList(crimeSituationPlotConvert.SituationIDList, convert.SituationIDList, "1");
                    }
                    else
                    {
                        crimeDataModelList.Add(crimeSituationPlotConvert);
                    }

                    SituationPlotConvert relevantSituationPlotConvert = PloConvert(elementModel.situationPlotResultModel.relevantDataModelList);
                    if (relevantDataModelList.FirstOrDefault(a => a.Name == crimeSituationPlotConvert.Name) != null)
                    {
                        SituationPlotConvert convert = relevantDataModelList.FirstOrDefault(a => a.Name == crimeSituationPlotConvert.Name);
                        CompareList(relevantSituationPlotConvert.plotIDList, convert.plotIDList, "2");
                        CompareList(relevantSituationPlotConvert.SituationIDList, convert.SituationIDList, "1");
                    }
                    else
                    {
                        relevantDataModelList.Add(relevantSituationPlotConvert);
                    }
                }

                crimeSituationPlotDataModels = ConvertSituationPlot(crimeDataModelList);
                relevantSituationPlotDataModels = ConvertSituationPlot(relevantDataModelList);
            }


        }


        /// <summary>
        /// 执行要素合并
        /// </summary>
        /// <param name="elementModelList"></param>
        public void Situation(List<ElementModel> elementModelList,string causeID)
        {
            if (elementModelList.Count > 0 && elementModelList.Where(a => a.situationPlotResultModel != null).ToList().Count > 0)
            {
                List<ElementModel> elementSituationList = elementModelList.Where(a => a.situationPlotResultModel != null).ToList();
                foreach (var elementModel in elementSituationList)
                {
                    Merge(elementModel.situationPlotResultModel.crimeDataModelList, crimeSituationPlotDataModels,causeID);
                    Merge(elementModel.situationPlotResultModel.relevantDataModelList, relevantSituationPlotDataModels, causeID);
                }
                SituationConvert(crimeSituationPlotDataModels);
                SituationConvert(relevantSituationPlotDataModels);
            }
        }

        /// <summary>
        /// 转换xml结果集合
        /// </summary>
        /// <param name="valueList"></param>
        public void SituationConvert(List<SituationPlotDataModel> valueList)
        {
            foreach (SituationPlotDataModel plotDataModel in valueList)
            {

                string xml = XmlUtility.SerializeToXmlNew<SituationModel>(plotDataModel.situationModel);
                plotDataModel.XmlDocument = new XmlDocument();
                plotDataModel.XmlDocument.LoadXml(xml);
                //plotDataModel.parndPath = plotDataModel.parndPath;
                XmlUtilCommon xmlUtilCommon = new XmlUtilCommon() { dataContext = dataContext };
                plotDataModel.dataModelList = xmlUtilCommon.GetXMLByDataModelBackID(plotDataModel.XmlDocument).dataModelList;
                plotDataModel.XmlDocument = xmlUtilCommon.DataModelByXml(plotDataModel.dataModelList, "");
            }

        }
        /// <summary>
        /// 情形情节合并方法
        /// </summary>
        /// <param name="crimeSituationModelList">被合并的数据集</param>
        /// <param name="relevantSituationModelList">结果数据集</param>
        public void Merge(List<SituationPlotDataModel> crimeSituationModelList, List<SituationPlotDataModel> relevantSituationModelList,string causeID)
        {
            var causeModel = dataContext.DataLayerAns.CaseCausesList.FirstOrDefault(a => a.CC_ID == causeID);
            foreach (var crimeDataModel in crimeSituationModelList)
            {
                if (relevantSituationModelList.FirstOrDefault(a => a.situationModel.Name == crimeDataModel.situationModel.Name) != null)
                {
                    SituationModel relevantSituationModel = relevantSituationModelList.FirstOrDefault(a => a.situationModel.Name == crimeDataModel.situationModel.Name).situationModel;

                    if (relevantSituationModel.plotModelList.Count > 0)
                    {
                        if (crimeDataModel.situationModel.plotModelList.Count > 0)
                        {
                            //情节信息
                            foreach (var plotDetil in crimeDataModel.situationModel.plotModelList[0].plotDetilModelList)
                            {
                                var plot = relevantSituationModel.plotModelList[0].plotDetilModelList.
                                    FirstOrDefault(a => a.PlotID == plotDetil.PlotID);
                                if (plot == null)
                                {
                                    plotDetil.Qjrdjg = "0";
                                    relevantSituationModel.plotModelList[0].plotDetilModelList.Add(plotDetil);
                                }
                                else
                                {
                                    plot.Qjrdjg = "1";
                                    if (plotDetil.plotOrgtextList.Any())
                                    {
                                        if (plot.plotOrgtextList == null)
                                        {
                                            plot.plotOrgtextList = new List<PlotDetilOrgtext>();
                                        }
                                        var exceptList = plotDetil.plotOrgtextList.Where(x => !plot.plotOrgtextList.Any(p => p.Orgtext == x.Orgtext)).ToList();
                                        plot.plotOrgtextList.AddRange(exceptList);
                                    }

                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (var plotDetil in crimeDataModel.situationModel.plotModelList)
                        {
                            foreach (var item in plotDetil.plotDetilModelList)
                            {
                                item.Qjrdjg = "0";
                            }
                        }
                        relevantSituationModel.plotModelList.AddRange(crimeDataModel.situationModel.plotModelList);
                    }
                    if (relevantSituationModel.situationList.Count > 0)
                    {
                        if (crimeDataModel.situationModel.situationList.Count > 0)
                        {
                            //情形信息
                            foreach (var situationDetil in crimeDataModel.situationModel.situationList[0].situationList)
                            {
                                var suituation = relevantSituationModel.situationList[0].situationList.
                                    FirstOrDefault(a => a.SituationID == situationDetil.SituationID);
                                if (suituation == null)
                                {
                                    situationDetil.Qxrdjg = "0";
                                    relevantSituationModel.situationList[0].situationList.Add(situationDetil);
                                }
                                else
                                {
                                    suituation.Qxrdjg = "1";
                                    if (situationDetil.situationOrgtextList.Any())
                                    {
                                        if (suituation.situationOrgtextList == null)
                                        {
                                            suituation.situationOrgtextList = new List<SituationDetilOrgtext>();
                                        }
                                        var exceptList = situationDetil.situationOrgtextList.Where(x => !suituation.situationOrgtextList.Any(p => p.Orgtext == x.Orgtext)).ToList();
                                        suituation.situationOrgtextList.AddRange(exceptList);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (var situationDet in crimeDataModel.situationModel.situationList)
                        {
                            foreach (var item in situationDet.situationList)
                            {
                                item.Qxrdjg = "0";
                            }
                        }
                        relevantSituationModel.situationList.AddRange(crimeDataModel.situationModel.situationList);
                    }


                }
                else
                {
                    //认定结果赋值
                    foreach (var detl in crimeDataModel.situationModel.situationList)
                    {
                        if (string.IsNullOrEmpty(detl.zmid))
                        {
                            if (causeModel!=null)
                            {
                                detl.zmid = causeModel.CC_ID;
                                detl.orgtext = causeModel.CC_Name;
                            }
                           
                        }
                            
                        foreach (var item in detl.situationList)
                        {
                            item.Qxrdjg = "0";
                        }
                    }
                    foreach (var detl in crimeDataModel.situationModel.plotModelList)
                    {
                        if (string.IsNullOrEmpty(detl.zmid))
                        {
                            if (causeModel != null)
                            {
                                detl.zmid = causeModel.CC_ID;
                                detl.orgtext = causeModel.CC_Name;
                            }
                        }
                        foreach (var item in detl.plotDetilModelList)
                        {
                            item.Qjrdjg = "0";
                        }
                    }
                    relevantSituationModelList.Add(crimeDataModel);
                }
            }
        }

        private List<SituationPlotDataModel> ConvertSituationPlot(List<SituationPlotConvert> comparisonDataModelList)
        {
            List<SituationPlotDataModel> situationPlotDataModels = new List<SituationPlotDataModel>();
            foreach (var item in comparisonDataModelList)
            {
                SituationModel situationModel = new SituationModel();
                situationModel.Name = item.Name;
                situationModel.plotModelList = new List<PlotModel>();
                situationModel.situationList = new List<Situation>();
                PlotModel plotModel = new PlotModel();
                plotModel.plotDetilModelList = new List<PlotDetilModel>();
                situationModel.plotModelList.Add(plotModel);
                foreach (var plot in item.plotIDList)
                {
                    PlotDetilModel plotDetilModel = new PlotDetilModel();
                    plotDetilModel.PlotID = plot;
                    plotModel.plotDetilModelList.Add(plotDetilModel);
                }

                situationModel.situationList = new List<Situation>();
                Situation situation = new Situation();

                situation.situationList = new List<SituationDetil>();
                situationModel.situationList.Add(situation);
                foreach (var suitid in item.SituationIDList)
                {
                    SituationDetil situationDetil = new SituationDetil();
                    situationDetil.SituationID = suitid;
                    situationDetil.Qxly = "";
                    situationDetil.Qxrdjg = "";
                    situation.situationList.Add(situationDetil);
                }
                SituationPlotDataModel situationPlotDataModel = new SituationPlotDataModel();
                situationPlotDataModel.situationModel = situationModel;
                string xml = XmlUtility.SerializeToXmlNew<SituationModel>(situationModel);
                situationPlotDataModel.XmlDocument = new XmlDocument();
                situationPlotDataModel.XmlDocument.LoadXml(xml);
                situationPlotDataModel.parndPath = item.ParedPath;
                XmlUtilCommon xmlUtilCommon = new XmlUtilCommon() { dataContext = dataContext };
                situationPlotDataModel.dataModelList = xmlUtilCommon.GetXMLByDataModelBackFill(situationPlotDataModel.XmlDocument).dataModelList;

                situationPlotDataModels.Add(situationPlotDataModel);
            }
            return situationPlotDataModels;

        }
        /// <summary>
        /// 比较匹配实体
        /// </summary>
        /// <param name="valueList">需要比较的集合</param>
        /// <param name="restList">返回的实体</param>
        /// <param name="type">1情形2情节</param>
        private void CompareList(List<string> valueList, List<string> restList, string type)
        {
            if (valueList.Count > 0)
            {
                if (restList.Count > 0)
                {
                    foreach (string plot in valueList)
                    {
                        if (restList.FirstOrDefault(a => a == plot) == null)
                        {
                            restList.Add(plot);
                        }
                        else
                        {
                            //添加到认定中
                        }
                    }
                }
                else
                {
                    restList = valueList;
                }
            }

        }


        /// <summary>
        /// 更新情形情节信息
        /// </summary>
        /// <param name="crimeList"></param>
        /// <param name="relevantList"></param>
        public void UpdateMerge(List<SituationPlotDataModel> crimeList, List<SituationPlotDataModel> relevantList, XmlDocument xmlDocument)
        {

            if (crimeList.Count > 0)
            {
                SituationConvert(crimeList);
                foreach (var crime in crimeList)
                {
                    string path = crime.parndPath.Replace("//","/");
                    if (!string.IsNullOrEmpty(path))
                    {
                        path = path.Replace("事实要素", "事实要素[@要素类型='2']");
                        XmlNode xmlNode = XmlUtility.SelectXMLPaht(xmlDocument, path);
                        XmlNode root = xmlDocument.ImportNode(crime.XmlDocument.SelectSingleNode("*"), true);
                        xmlNode.AppendChild(root);
                    }

                }
                SituationConvert(crimeList);
                foreach (var crime in crimeList)
                {
                    string path = crime.parndPath.Replace("//", "/");
                    if (!string.IsNullOrEmpty(path))
                    {
                        path = path.Replace("事实要素", "事实要素[@要素类型='4']");
                        XmlNode xmlNode = XmlUtility.SelectXMLPaht(xmlDocument, path);
                        XmlNode root = xmlDocument.ImportNode(crime.XmlDocument.SelectSingleNode("*"), true);
                        xmlNode.AppendChild(root);
                    }
                }
            }

            if (relevantList.Count > 0)
            {
                SituationConvert(relevantList);
                foreach (var crime in relevantList)
                {
                    string path = crime.parndPath.Replace("//", "/");
                    if (!string.IsNullOrEmpty(path))
                    {
                        path = path.Replace("事实要素", "事实要素[@要素类型='2']");
                        XmlNode xmlNode = XmlUtility.SelectXMLPaht(xmlDocument, path);
                        XmlNode root = xmlDocument.ImportNode(crime.XmlDocument.SelectSingleNode("*"), true);
                        xmlNode.AppendChild(root);
                    }

                }
                SituationConvert(relevantList);
                foreach (var crime in relevantList)
                {
                    string path = crime.parndPath.Replace("//", "/");
                    if (!string.IsNullOrEmpty(path))
                    {
                        path = path.Replace("事实要素", "事实要素[@要素类型='4']");
                        XmlNode xmlNode = XmlUtility.SelectXMLPaht(xmlDocument, path);
                        XmlNode root = xmlDocument.ImportNode(crime.XmlDocument.SelectSingleNode("*"), true);
                        xmlNode.AppendChild(root);
                    }

                }
            }
        }

        /// <summary>
        /// 情形情节转换
        /// </summary>
        /// <param name="comparisonDataModelList">需要转换的实体</param>
        private SituationPlotConvert PloConvert(List<SituationPlotDataModel> comparisonDataModelList)
        {
            SituationPlotConvert situationPlotConvert = new SituationPlotConvert();
            if (comparisonDataModelList.Count > 0)
            {
                foreach (var crimeModel in comparisonDataModelList)
                {
                    if (crimeModel.situationModel != null)
                    {
                        situationPlotConvert.Name = crimeModel.situationModel.Name;
                        situationPlotConvert.ParedPath = crimeModel.parndPath;
                        foreach (var situationPlotDetil in comparisonDataModelList)
                        {
                            if (situationPlotDetil.situationModel.plotModelList.Count > 0)
                            {
                                List<string> list = new List<string>();
                                foreach (var situation in situationPlotDetil.situationModel.plotModelList[0].plotDetilModelList)
                                {
                                    string situationID = situation.PlotID;
                                    list.Add(situationID);
                                }
                                situationPlotConvert.SituationIDList = list;
                            }
                            if (situationPlotDetil.situationModel.situationList.Count > 0)
                            {
                                List<string> list = new List<string>();
                                foreach (var situation in situationPlotDetil.situationModel.situationList[0].situationList)
                                {
                                    string situationID = situation.SituationID;
                                    list.Add(situationID);
                                }
                                situationPlotConvert.plotIDList = list;
                            }
                        }
                    }
                }
            }
            return situationPlotConvert;
        }
    }

    /// <summary>
    /// 人员对应诉讼地位实体
    /// </summary>
    public class SituationPlotConvert
    {

        /// <summary>
        /// 情形ID集合
        /// </summary>
        public List<string> SituationIDList = new List<string>();

        /// <summary>
        /// 情节
        /// </summary>
        public List<string> plotIDList = new List<string>();
        /// <summary>
        /// 人员姓名
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 人员姓名
        /// </summary>
        public string StatusID { get; set; }

        /// <summary>
        /// 父级节点路径
        /// </summary>
        public string ParedPath { get; set; }
    }

}
